package be.redo.controller;

import be.redo.model.entities.Employee;
import be.redo.service.EmployeeService;
import be.redo.service.VacationService;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RequestMapping("/employees")
@Controller
public class EmployeeController {

	@Autowired
	EmployeeService emplyeeService;

	@Autowired
	VacationService vacationService;

	@RequestMapping(method = RequestMethod.POST, produces = "text/html")
	public String create(@Valid Employee employee, BindingResult bindingResult,
			Model uiModel, HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, employee);
			return "employees/create";
		}
		uiModel.asMap().clear();
		emplyeeService.create(employee);
		return "redirect:/employees/"
				+ encodeUrlPathSegment(employee.getEmployeeId().toString(),
						httpServletRequest);
	}

	@RequestMapping(params = "form", produces = "text/html")
	public String createForm(Model uiModel) {
		populateEditForm(uiModel, new Employee());
		return "employees/create";
	}

	@RequestMapping(value = "/{EmployeeId}", produces = "text/html")
	public String show(@PathVariable("EmployeeId") Long EmployeeId,
			Model uiModel) {
		uiModel.addAttribute("employee", emplyeeService.read(EmployeeId));
		uiModel.addAttribute("itemId", EmployeeId);
		return "employees/show";
	}

	/*
	 * @RequestMapping(produces = "text/html") public String
	 * list(@RequestParam(value = "page", required = false) Integer page,
	 * @RequestParam(value = "size", required = false) Integer size, Model
	 * uiModel) { if (page != null || size != null) { int sizeNo = size == null
	 * ? 10 : size.intValue(); final int firstResult = page == null ? 0 :
	 * (page.intValue() - 1) * sizeNo; uiModel.addAttribute("employees",
	 * emplyeeService.findEmployeeEntries(firstResult, sizeNo)); float nrOfPages
	 * = (float) emplyeeService.countAllEmployees() / sizeNo;
	 * uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages ||
	 * nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages)); } else {
	 * uiModel.addAttribute("employees", emplyeeService.findAllEmployees()); }
	 * return "employees/list"; }
	 */
	@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
	public String update(@Valid Employee employee, BindingResult bindingResult,
			Model uiModel, HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, employee);
			return "employees/update";
		}
		uiModel.asMap().clear();
		emplyeeService.update(employee);
		return "redirect:/employees/"
				+ encodeUrlPathSegment(employee.getEmployeeId().toString(),
						httpServletRequest);
	}

	@RequestMapping(value = "/{EmployeeId}", params = "form", produces = "text/html")
	public String updateForm(@PathVariable("EmployeeId") Long EmployeeId,
			Model uiModel) {
		populateEditForm(uiModel, emplyeeService.read(EmployeeId));
		return "employees/update";
	}

	@RequestMapping(value = "/{EmployeeId}", method = RequestMethod.DELETE, produces = "text/html")
	public String delete(@PathVariable("EmployeeId") Long EmployeeId,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel) {
		Employee employee = emplyeeService.read(EmployeeId);
		emplyeeService.delete(employee.getEmployeeId());
		uiModel.asMap().clear();
		uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
		uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/employees";
	}

	void populateEditForm(Model uiModel, Employee employee) {
		uiModel.addAttribute("employee", employee);
		uiModel.addAttribute("vacations", vacationService.findAll());
	}

	String encodeUrlPathSegment(String pathSegment,
			HttpServletRequest httpServletRequest) {
		String enc = httpServletRequest.getCharacterEncoding();
		if (enc == null) {
			enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
		}
		try {
			pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
		} catch (UnsupportedEncodingException uee) {
		}
		return pathSegment;
	}
}
